package com.yuanian.ecsplus.converter;

import com.yuanian.businessobject.model.entity.base.BOMRefValueBean;
import com.yuanian.dp.common.vo.CustomeFieldValueVo;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
 * 类型转换器的工厂类
 */
public class TypeConverterFactory {

    //key为字段的类型
    private static final Map<Class<?>, TypeConverter> typeConverterMap = new HashMap<>();

    static {
        BooleanTypeConverter booleanTypeConverter = new BooleanTypeConverter();
        addTypeConverter(Boolean.class, booleanTypeConverter);
        addTypeConverter(boolean.class, booleanTypeConverter);
        addTypeConverter(Date.class, new DateTypeConverter());
        addTypeConverter(CustomeFieldValueVo.class, new CustomerFieldValueVoConverter());
        addTypeConverter(BOMRefValueBean.class, new BOMRefValueBeanTypeConverter());
        addTypeConverter(Other.class, new CommonTypeConverter());
    }

    /**
     * 根据字段的类型获取类型转换器
     * @param clz
     * @return
     */
    public static TypeConverter getTypeConverter(Class clz) {
        TypeConverter typeConverter = (TypeConverter)typeConverterMap.get(clz);
        return typeConverter == null ? (TypeConverter)typeConverterMap.get(Other.class) : typeConverter;
    }

    /**
     * 加入类型转换器
     * @param clz
     * @param typeConverter
     */
    public static void addTypeConverter(Class clz, TypeConverter typeConverter) {
        typeConverterMap.putIfAbsent(clz, typeConverter);
    }

    class Other {

    }
}
